//数据示例
// var xxx = {
//     status: 0,
//     approvers: [
//         {
//             name: "申请人",
//             extras: [{name: "申请时间", value: "2018-05-07 18:20:30"}, {name: "合同", value: "合同吗"}],
//             status: 1
//         },
//         {
//             name: "shenpiren1",
//             extras: [{name: "申请时间s 是", value: "2018-05-07 18:20:30"}, {name: "合同", value: "1111"}],
//             status: 0
//         },
//         {
//             name: "sdfa",
//             extras: [{name: "申请时间", value: "2018-05-07 18:20:30"}, {name: "合同", value: "1111"}],
//             status: 2
//         },
//         {
//             name: "shenpirvzvdfen1",
//             extras: [{name: "申请时间", value: "2018-05-07 18:20:30"}, {name: "合同", value: "1111"}],
//             status: 0
//         }
//     ]
// }

function initViewApprover(info) {
    function getCloneData(id) {
        var clone = $("#" + id).clone(true);
        clone.removeAttr("id");
        return clone;
    }
    function initFieds(info){
        var approver_config=$("#approver_config");
        approver_config.empty();
        approver_config.append(getCloneData("approval_begin_pass"));

        var models = [getCloneData("approval_approver_default"),getCloneData("approval_approver_default"), getCloneData("approval_approver_passed"), getCloneData("approval_approver_error")]

        $(models[0]).find(".approval_approver").css("background-color","#00FF00");

        if (info.approvers) {
            info.approvers.forEach(function (e) {
                var node = $(models[e.status+1]).clone();
                node.find(".approver_name_class").html(e.name);
                var hide = node.find(".approve_extra_tr_hide");
                if (e.extras && hide) {
                    e.extras.forEach(function (extra) {
                        var tr = hide.clone();
                        tr.removeClass("approve_extra_tr_hide")
                        tr.find(".approve_extra_td_left").html(extra.name + ":");
                        tr.find(".approve_extra_td_right").html(extra.value);
                        tr.insertBefore(hide);
                    })
                    hide.remove();
                }
                approver_config.append(node);
            })
        }
        approver_config.append(getCloneData(info.status == 1 ? "approval_end_pass" : "approval_end_default"));
    }

    function changeHeight() {
        var groupNum=1;
        function needChangeHeight() {
            var ifResized = false;
            var preGroupTop=null, preTop = null;
            for (var i = 0; i < groupNum && !ifResized; i++) {
                $("#approver_config .approval_approver_body[groupNum=" + i + "]").each(function (n, e) {
                    var top = $(e).position().top;
                    if (n == 0) {
                        if (i != 0) {
                            preGroupTop = preTop;
                        }
                        preTop = top;
                        if (preGroupTop == top) {
                            ifResized = true;
                            return false;
                        }
                    }

                    if (preTop != top) {
                        ifResized = true;
                        return false;
                    }
                })
            }
            return ifResized;
        }
        function changeHeightOnResize() {
            if(!needChangeHeight()){
                return
            }
            var topIndexArr=[],topObj={},objTable={},minTop=Number.MAX_SAFE_INTEGER;
            $("#approver_config .approval_approver_body").each(function () {
                var top = $(this).position().top;
                var index=topIndexArr.indexOf(top);
                if(index==-1){
                    topIndexArr.push(top);
                    index=topIndexArr.indexOf(top);
                    topObj[index]=[];
                    objTable[index]=[];
                }
                $(this).attr("groupNum",index)

                topObj[index].push(this);
                objTable[index].push($(this).find(".approval_approver_table"));
            })
            groupNum=topIndexArr.length;

            for (var name = 0; name < groupNum; name++) {
                var paddingTop = name == 0 ? "0px" : "10px";
                $.each(topObj[name], function () {
                    $(this).css("padding-top", paddingTop)
                })
                toMaxHeight(topObj[name], getMaxHeight(objTable[name]))
            }
        }
        $("#approver_config .approval_approver_body").attr("groupNum",0);
        setTimeout(function () {changeHeightOnResize()},50);
        $(window).resize(function () {
            changeHeightOnResize()
        })
    }
    initFieds(info);
    changeHeight();
}
